package com.system.blogAdmin.auth.service.impl;

import com.cnc.common.persistence.biz.BaseBizImpl;
import com.cnc.common.persistence.dao.BaseDao;
import com.cnc.common.persistence.exception.BizException;
import com.cnc.common.web.util.MessageBox;
import com.system.blogAdmin.auth.dto.UserDto;
import com.system.blogAdmin.auth.entity.AuthResource;
import com.system.blogAdmin.auth.entity.User;
import com.system.blogAdmin.auth.dao.UserDao;
import com.system.blogAdmin.auth.kit.ShiroKit;
import com.system.blogAdmin.auth.service.AuthResourceService;
import com.system.blogAdmin.auth.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.shiro.authc.UnknownAccountException;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @描述：[BEAN_NAME]Service实现�?
 * @作�??:  Auto Code
 * @创建时间:  2017-4-22 10:10:43
 * @版本: 1.0
 */
@Service("userService")
public class UserServiceImpl extends BaseBizImpl<User> implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;
    @Autowired
    AuthResourceService authResourceService;

    @Override
    protected BaseDao<User> getDao() {
        return userDao;
    }
    @Override
    public User getByAccount(String username){
        return userDao.getByAccount(username);
    }

    @Override
    public List<AuthResource> listResourseByUserId(Long userId){
        return userDao.listResourseByUserId(userId);
    }

    @Override
    public User login(String account, String password){
            User user = this.getByAccount(account);
            if(user == null){
                logger.info("找不到该用户");
                throw new UnknownAccountException("找不到该用户!");
            }
            if ( !user.getPassword().equals(ShiroKit.md5(password, account + password)) ){
                logger.info("用户名或密码错误");
                throw new UnknownAccountException("用户名或密码错误!");
            }
            if( user.getIsLock() == null || user.getIsLock() == 1 ){
                logger.info("账号已被锁定");
                throw new LockedAccountException("账号已被锁定!");
            }
            return user;
    }

    @Override
    public List<UserDto> getAllUser(){
        return userDao.getAllUser();
    }

    @Override
    public void insertUser(User user) {
        if(userDao.getByAccount(user.getAccount()) != null){
            throw new BizException("已存在该账号名");
        }
        //加密
        user.setPassword(ShiroKit.md5(user.getPassword(), user.getAccount() + user.getPassword()));
        userDao.insert(user);
    }

    @Override
    public List<AuthResource> listMenuInfoByUserId(Long userId) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        //超级管理员
        if(user.getAccount().equals(User.ADMIN_USER_NAME)){
            return  authResourceService.authResourceList(AuthResource.MENU_RESOURCE);
        }
        //通过用户ID 获取 用户资源列表
        List<AuthResource> resourceslist = authResourceService.listResourseByUserId(userId);
        //过滤出菜单资源的列表
        List<AuthResource> list = resourceslist.stream().filter(resource ->
                resource.getType().equals(AuthResource.MENU_RESOURCE)).collect(Collectors.toList());
        return  list;
    }

    public static void main(String[] args) {
        String str = ShiroKit.md5("123", "cnc123");
        System.out.println(str);
    }
}
